<?php

namespace App\V3\Api\Services;

use App\V3\Api\Models\AdminAddendumModel;
use App\V3\Api\Models\StorageManageModel;
use App\V3\Api\Models\StorageShelfLocationModel;
use App\V3\Api\Models\StorageShelfModel;
use Illuminate\Support\Facades\DB;

class ShelfService
{
    public static function get_list_by_main_id($main_id,$data)
    {
        $list = StorageShelfModel::select(
            'id',
            'shelf_name',
            'shelf_type',
        )
            ->where([
                'status' => StorageShelfModel::USEDSTATUS,
                'main_id' => $main_id,
                'storage_id' => $data['storageId']
            ])
            ->with('locations', function($query) {
                $query->select(
                    'id',
                    'location_name',
                    'shelf_id',
                    'storage_id'
                )->where([
                    'status' => StorageShelfLocationModel::USEDSTATUS
                ]);
            })
            ->orderBy('id','desc')->get()->toArray();

        return $list;
    }

    //用于删除功能判断库存信息 返回1 可以删除 0不能删除
    public static function judging_del($id,$id_type,$main_id)
    {
        $res = StorageManageModel::select('*')->where('delete_flag',0);
        switch ($id_type)
        {
            case 1: //删除仓库使用
                $res = $res->where('storage_id', $id);
                break;
            case 2: //删除货架使用
                $res = $res->where('shelf_id', $id);

                break;
            case 3: //删除库位使用
                $res = $res->where('location_id', $id);
                break;
            default:
                $res = $res->where('storage_id', $id);
        }
        $res = $res->where('main_id',$main_id);

        $res = $res->where('in_storage_num','!=',0)->first();

        if(empty($res)){
            return 1;
        }else{
            return 0;
        }
    }

    //创建货架和库位
    public static function create_default_shelf_location($storage_id,$main_id,$type = 1,$shelf_name='默认货架',$location_name='默认库位')
    {
        DB::beginTransaction();

        try {
            $shelf_obj = StorageShelfModel::updateOrCreate([
                'storage_id' => $storage_id,
                'main_id' => $main_id,
                'status' => StorageShelfModel::USEDSTATUS,

            ],[
                'shelf_name' => $shelf_name,
                'main_id' => $main_id,
                'storage_id' => $storage_id,
                'status' => StorageShelfModel::USEDSTATUS,
                'shelf_type' => $type,
            ]);

            $shelf_id = $shelf_obj->id;
            $location_obj = StorageShelfLocationModel::updateOrCreate([
                'storage_id' => $storage_id,
                'main_id' => $main_id,
                'shelf_id' => $shelf_id,
                'status' => StorageShelfModel::USEDSTATUS

            ],[
                'location_name' => $location_name,
                'main_id' => $main_id,
                'storage_id' => $storage_id,
                'shelf_id' => $shelf_id,
                'status' => StorageShelfModel::USEDSTATUS,
                'shelf_type' => $type,

            ]);

            DB::commit();
        } catch (\Exception $e) {
            DB::rollBack();
        }
    }

    //判断是否开启货架功能
    public static function is_use_shelf($main_id)
    {
        $info = AdminAddendumModel::select('*')->where(['main_id' => $main_id])->first();
        if(empty($info['is_use_shelf'])){
            return 0;
        }else{
            return 1;
        }
    }

    //获取默认货架和库位
    public static function get_default($id,$type = 1)
    {
        if($type == 1){
            //获取默认货架
            $data = StorageShelfModel::select(
                'id',
                'shelf_name',
            )->where([
                'status' => StorageShelfModel::USEDSTATUS,
                'storage_id' => $id,
                'shelf_type' => StorageShelfModel::DEFAULTTYPE,
            ])->orderBy('id', 'asc')
                ->first();
        }else{
            //获取库位
            $data = StorageShelfLocationModel::select(
                'id',
                'location_name',
            )->where([
                'status' => StorageShelfLocationModel::USEDSTATUS,
                'shelf_id' => $id,
                'location_type' => StorageShelfLocationModel::DEFAULTTYPE,
            ])->orderBy('id', 'asc')
                ->first();
        }

        return $data;
    }

    public static function shelf_list($main_id,$data)
    {
        $list = StorageShelfModel::select(
            'id',
            'shelf_name',
            'shelf_type',
        )->where([
            'status' => StorageShelfModel::USEDSTATUS,
            'storage_id' => $data['storageId'],
            'main_id' => $main_id,
        ]);

        if(!empty($data['searchData'])){
            $list = $list->where('shelf_name','LIKE',"%{$data['searchData']}%");
        }

        return $list->orderBy('sort', 'DESC')->get()->toArray();
    }

    public static function location_list($main_id,$data)
    {
        $list = StorageShelfLocationModel::select(
            'id',
            'location_name',
            'location_type',
            'storage_id',
            'shelf_id'
        )->where([
            'status' => StorageShelfLocationModel::USEDSTATUS,
            'shelf_id' => $data['shelfId'],
            'main_id' => $main_id,
        ]);

        if(!empty($data['searchData'])){
            $list = $list->where('location_name','LIKE',"%{$data['searchData']}%");
        }

        $list = $list->orderBy('sort', 'DESC')->get()->toArray();

        foreach ($list as &$v){
            $storage_manage = StorageManageModel::where(
                [
                    'storage_id' => $v['storage_id'],
                    'shelf_id' => $v['shelf_id'],
                    'location_id' => $v['id'],
                ])->first();

            unset($v['shelf_id']);
            unset($v['storage_id']);

            $v['is_null'] = empty($storage_manage) ? 1 : 0;
        }

        return $list;
    }
}
